<?php
namespace app\admin\controller;

class Module extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'Module';
    }
    //
    public function index(){
        $this->_before_index();
        $model = model($this->model);
        $list = [];
        $x = $y = $z = 0;
        $father = $model->where('fid', '=', 0)->select()->toArray();
        foreach($father as $key => $row){
            $x++;
            $row['tab'] = '';
            $list[] = $row;
            $sons = $model->where('fid', '=', $row['id'])->select()->toArray();
            foreach($sons as $value){
                $y++;
                $value['tab'] = '|-';
                $list[] = $value;
                $gsons = $model->where('fid', '=', $value['id'])->select()->toArray();
                foreach($gsons as $val){
                    $z++;
                    $val['tab'] = '&nbsp;&nbsp;|-';
                    $list[] = $val;
                }
            }
        }
        $list = $this->_after_list($list);
        $this->assign('list', $list);
        $this->assign('totalCount', ['x' => $x, 'y' => $y, 'z' => $z]);
        return $this->fetch();
    }
    //
    protected function _before_index(){
        $thead = [
            'id' => 'ID',
            'title' => '中文名称',
            'module' => '模块名称',
            'fid' => '父级模块',
            'faicon' => '模块图标',
            'sort' => '排列顺序',
            'addtime' =>'创建时间',
        ];
        $this->assign('thead', $thead);
    }
    //
    protected function _filter($map){
        return $map;
    }
    //
    protected function _after_list($list){
        $fidArr = [];
        foreach($list as $row){
            $fidArr[] = $row['fid'];
        }
        $fArr = model($this->model)->where('id', 'in', $fidArr)->column('title', 'id');
        foreach($list as $key => $row){
            $list[$key]['title'] = $row['tab'].$row['title'];
            $list[$key]['fid'] = $fArr[$row['fid']] ?? '顶级模块';
            $list[$key]['addtime'] = date('Y-m-d H:i:s', $row['addtime']);
        }
        return $list;
    }
    //
    protected function _before_add(){
        $fArr = model($this->model)->where('fid', '=', 0)->field('id,title')->select()->toArray();
        $this->assign('fArr', array_merge([['id'=>0,'title'=>'顶级模块']],$fArr));
        $sArr = [];
        $sArr['0'] = array_merge([['id'=>0,'title'=>'顶级模块']],$fArr);
        foreach($fArr as $key => $row){
            $sArr[$row['id']] = model($this->model)->where('fid', '=', $row['id'])->field('id,title')->select()->toArray();
        }
        $this->assign('sArr', $sArr);
    }
    //
    protected function _before_insert($data){
        $data['addtime'] = time();
        return $data;
    }
    //
    protected function _after_add($data, $lastid){
        $this->log(['pk' => 'id', 'value' => $lastid], $data);
    }
    //
    protected function _before_edit(){
        $this->_before_add();
    }
    //
    protected function _filter_edit($list){
        $list['ffid'] = $list['fid'] != 0 ? model($this->model)->where('id', '=', $list['fid'])->value('fid') : '0';
        return $list;
    }
    //
    protected function _before_save($data){
        return $data;
    }
    //
    protected function _after_edit($data){
        $this->log(['pk' => 'id', 'value' => $data['id']], $data);
    }
    //
    protected function _filter_del(){
        return [1,6,14,37];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'id', 'value' => $row['id'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}